package com.dzqc.fly.controller.mobile;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.dzqc.fly.common.NumberUtil;
import com.dzqc.fly.common.PageUtils;
import com.dzqc.fly.common.Result;
import com.dzqc.fly.db.form.*;
import com.dzqc.fly.service.AppTeacherService;
import com.dzqc.fly.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jdk.nashorn.internal.ir.CallNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedList;


@RestController
@RequestMapping("/appteacher")
@Tag(name = "AppTeacherController", description = "APP教师端相关")
@Slf4j
@EnableAutoConfiguration
public class AppTeacherController {

    @Autowired
    private UserService userService;

    @Autowired
    private AppTeacherService appTeacherService;

    @RequestMapping(value="/searchIndexDataOfTeacher" ,method = RequestMethod.POST)
   // @PostMapping("/searchIndexDataOfTeacher")
    @SaCheckLogin
    @Operation(summary = "查询教师端首页列表数据")
    public Result searchIndexDataOfTeacher(@Valid @RequestBody searchIndexDataOfTeacherForm form){
        int page=form.getPage();
        int length=form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("sno",userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno"));

        PageUtils pageUtils=appTeacherService.searchIndexDataOfTeacher(param);
        return Result.ok().put("page",pageUtils);
    }

    @RequestMapping(value="/getLeaveDetails" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveDetails")
    @Operation(summary = "查询教师端学生请假记录详情")
    public Result getLeaveDetails(@Valid @RequestBody searchLeaveDetailForm form){
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        HashMap resultMap = appTeacherService.getLeaveDetails(param);
        LinkedList<HashMap> hashMaps = appTeacherService.getLeaveDetailFiles(param);
        if (hashMaps != null) {
            resultMap.put("list",hashMaps);
        }
        else if (hashMaps == null) {
            resultMap.put("list"," ");
        }
        return Result.ok().put("result",resultMap);
    }

    @RequestMapping(value="/updateLeave" ,method = RequestMethod.POST)
   // @PostMapping("/updateLeave")
    @SaCheckLogin
    @Operation(summary = "辅导员审批接口(需填写审批意见)")
    public Result updateLeave(@Valid @RequestBody updateLeaveForm form){
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        long hours = appTeacherService.searchTotalhours(param);
        if (hours> 0&&hours<=72) {
            if (3== (Integer) (param.get("shenpistatus"))) {
                param.put("leavestatus",5);
            }
            else{
                param.put("leavestatus",1);
            }
        }
        else if (hours > 72) {
            if (6== (Integer) (param.get("shenpistatus"))) {
                param.put("leavestatus",5);
            }
            else{
                param.put("leavestatus",1);
            }
        }
        else if(hours<=0){
            return Result.error();
        }

        int Leaverows=appTeacherService.updateLeave(param);
        int rowss=appTeacherService.updateLeaveshenpi(param);
        return Result.ok().put("Leaverows",Leaverows) .put("shenpirows",rowss);
    }

    @RequestMapping(value="/getLeaveTeacherhandling" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveTeacherhandling")
    @SaCheckLogin
    @Operation(summary = "查询教师端处理中的列表数据")
    public Result getLeaveTeacherhandling(@Valid @RequestBody searchIndexDataOfTeacherForm form){
        int page=form.getPage();
        int length=form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("sno",userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno"));

        PageUtils pageUtils=appTeacherService.getLeaveTeacherhandling(param);
        return Result.ok().put("page",pageUtils);
    }

    @RequestMapping(value="/getLeaveingTeacherDataDetails" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveingTeacherDataDetails")
    @SaCheckLogin
    @Operation(summary = "查询教师端请假中的列表数据")
    public Result getLeaveingTeacherDataDetails(@Valid @RequestBody searchIndexDataOfTeacherForm form){
        int page=form.getPage();
        int length=form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("sno",userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno"));

        PageUtils pageUtils=appTeacherService.getLeaveingTeacherDataDetails(param);
        return Result.ok().put("page",pageUtils);
    }

    @RequestMapping(value="/getLeaveFinishedTeacherDataDetails" ,method = RequestMethod.POST)
   // @PostMapping("/getLeaveFinishedTeacherDataDetails")
    @SaCheckLogin
    @Operation(summary = "查询教师端请销假记录的列表数据")
    public Result getLeaveFinishedTeacherDataDetails(@Valid @RequestBody searchIndexDataOfTeacherForm form){
        int page=form.getPage();
        int length=form.getLength();
        int start=(page-1)*length;
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start",start);
        param.put("sno",userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno"));

        PageUtils pageUtils=appTeacherService.getLeaveFinishedTeacherDataDetails(param);
        return Result.ok().put("page",pageUtils);
    }

    @RequestMapping(value="/getclassname" ,method = RequestMethod.GET)
    //@GetMapping("/getclassname")
    @SaCheckLogin
    @Operation(summary = "教师端获取名下班级")
    public Result getclassName(){
        HashMap param= new HashMap();
        param.put("fdyid",userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno"));
        LinkedList<HashMap> resultMap = appTeacherService.getclassNameOfTeacherStatistics(param);
        return Result.ok().put("result",resultMap);
    }

    @RequestMapping(value="/getLeaveAllStudentOfTeacherStatistics" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveAllStudentOfTeacherStatistics")
    @SaCheckLogin
    @Operation(summary = "获取教师端每个班级下请假情况")
    public Result getLeaveStudentOfTeacherStatistics(@Valid @RequestBody getLeaveStudentOfTeacherStatisticsForm form){

        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        LinkedList list=appTeacherService.getLeaveAllStudentOfTeacherStatistics(param);
        return Result.ok().put("list",list);
    }

    @RequestMapping(value="/getLeaveOneStudentOfTeacherStatistics" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveOneStudentOfTeacherStatistics")
    @SaCheckLogin
    @Operation(summary = "获取教师端某个学生的请假情况")
    public Result getLeaveOneStudentOfTeacherStatistics(@Valid @RequestBody getLeaveOneStudentOfTeacherStatisticsForm form){

        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        LinkedList<HashMap> list=appTeacherService.getLeaveOneStudentOfTeacherStatistics(param);
        HashMap studentInfo = appTeacherService.getStudentInfo(param);
        if(list.size()>0){
            long totalHours=appTeacherService.getSumOfStudentLeaveHours(param);//请假总时长
            return Result.ok().put("list",list).put("totalHours",totalHours).put("studentInfo",studentInfo);
        }
        return Result.ok().put("list",list).put("totalHours",0).put("studentInfo",studentInfo);

    }

    @RequestMapping(value="/getMonthOfTeacher" ,method = RequestMethod.POST)
   // @GetMapping("/getMonthOfTeacher")
    @SaCheckLogin
    @Operation(summary = "教师端查询学生请假月份(统计)")
    public Result searchMonth(){
        Object sno = userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno");
        LinkedList<HashMap> resultMap = appTeacherService.getMonthOfTeacher(NumberUtil.convertToInt(sno));
        return Result.ok().put("result",resultMap);
    }

    @RequestMapping(value="/getLeaveList" ,method = RequestMethod.POST)
    //@PostMapping("/getLeaveList")
    @SaCheckLogin
    @Operation(summary = "获取教师端某个月份学生的请假情况")
    public Result getLeaveList(@Valid @RequestBody getLeaveListForm form){

        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        Object fdyid = userService.searchUserInfo(StpUtil.getLoginIdAsInt()).get("sno");
        param.put("fdyid",NumberUtil.convertToInt(fdyid));
        LinkedList list=appTeacherService.getLeaveList(param);
        return Result.ok().put("list",list);
    }
}
